import { StringOrNumber } from "./type-aliases";

export class RangeValidationBase {
    constructor(private start: number, private end: number) { }
    protected RangeCheck(value: number): boolean {
        return value >= this.start && value <= this.end;
    }
    protected GetNumber(value: string): number {
        return new Number(value).valueOf();
    }
}

class SeparateTypeRangeValidation extends RangeValidationBase {
    IsInRangeString(value: string): boolean {
        return this.RangeCheck(+value)
    }
    isInRangeNumber(value: number): boolean {
        return this.RangeCheck(value)
    }
}

class AnyRangeValidation extends RangeValidationBase {
    IsInRange(value: any): boolean {
        if(typeof value === 'number'){
            return this.RangeCheck(value)
        }else if(typeof value === "string"){
            return this.RangeCheck(this.GetNumber(value));
        }
        return false;
    }
}

class UnionRangeValidation extends RangeValidationBase{
    IsInRange(value: string | number){
        if(typeof value === "number"){
            return this.RangeCheck(value);
        }
        return this.RangeCheck(this.GetNumber(value));
    }
}

let anyValidaor  = new AnyRangeValidation(10,20);

let validation = function(input : any): void{
    if(anyValidaor.IsInRange(input)){
        console.log(`${input} is in the range 10 to 20`);
    }else{
        console.log(`${input} is not in the range 10 to 20`)
    }
}
console.log(`starting any validations..`)
validation("133")
validation("133.5")
validation("13.55")
validation(19)
validation(true)
console.log(`ending`)
let unionValidation = new UnionRangeValidation(10,20);
let validation2 = function(input : string | number) {
    if (unionValidation.IsInRange(input)) {
        console.log(`${input} is in the range 10 to 20`);
    } else {
        console.log(`${input} is not in the range 10 to 20`)
    }
}
console.log(`starting union validations..`)
validation2("133")
validation2("133.5")
validation2("13.55")
validation2(19)
// validation2(true)
console.log(`ending`)